package com.da.javatest.design.schema.singleton;

public class SingletonTest05 {
    private static SingletonTest05 instance;

    private SingletonTest05() {}

    //提供一个静态的公有方法，当使用到该方法时，才去创建 instance
    //即懒汉式
    public static SingletonTest05 getInstance() {
        if (instance == null) {
            synchronized (SingletonTest05.class) {
                instance = new SingletonTest05();
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        System.out.println("懒汉式3 ， 线程不安全~");
        SingletonTest05 instance = SingletonTest05.getInstance();
        SingletonTest05 instance2 = SingletonTest05.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}
